/****************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxprodrsapi.p                                    */
/* PROCEDURE TYPE        : API export Process                               */
/* DESCRIPTION           : production schedule export Process               */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                 5-Mar-24  */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}
{us/px/pxmaint.i}
{us/px/pxphdef.i sosodxr}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical initial yes      no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram         as character                no-undo.
define variable vc_traceid            as character                no-undo.

define variable site            like wo_site      no-undo.
define variable site1           like wo_site      no-undo.
define variable prline          like rps_line     no-undo.
define variable prline1         like rps_line     no-undo.
define variable release_date    like wr_start     no-undo.
define variable release_date1   like ro_end       no-undo.
define variable primary_seq     like lnd_run_seq1 no-undo.
define variable primary_seq1    like lnd_run_seq1 no-undo.
define variable primary_sort    like mfc_logical  no-undo
   initial yes format "Ascending/Descending".
define variable secondary_seq   like lnd_run_seq2 no-undo.
define variable secondary_seq1  like lnd_run_seq2 no-undo.
define variable secondary_sort  like mfc_logical  no-undo
   initial yes format "Ascending/Descending".
define variable old_start       like release_date no-undo initial today.
define variable open_qty        like rps_qty_req  no-undo.
define variable l_site          as  character     format "x(8)"  no-undo.




define temp-table ttSearchCondition no-undo

        field site_from             as character
        field site_to               as character
        field productionline_from   as character
        field productionline_to     as character
        field part_from             as character
        field part_to               as character
        field date_from             as date 
        field date_to               as date 
        .


 define temp-table ttproductionrs no-undo

        field schd_site           like si_mstr.si_site
        field schd_part           like rps_mstr.rps_part    
        field schd_line           like ln_mstr.ln_line
        field schd_rlse_date      like rps_mstr.rps_rel_date
        field schd_due_date       like rps_mstr.rps_rel_date
        field schd_run_seq1       like lnd_det.lnd_run_seq1
        field schd_run_seq2       like lnd_det.lnd_run_seq1
        field schd_qty_req        like rps_mstr.rps_qty_req
        field schd_qty_comp       like rps_mstr.rps_qty_comp
        field schd_qty_open       like rps_mstr.rps_qty_comp

        index idx1 schd_site schd_part schd_line schd_rlse_date.

                    

  
   DEFINE DATASET dsSearchCondition FOR ttSearchCondition.

   hdataset = dataset dsSearchCondition:HANDLE.   
           
   DEFINE DATASET dsproductionrs FOR ttproductionrs.

   hresult = dataset dsproductionrs:HANDLE.    
                
output to /apps/eai/work/cim/prodrs.log.
mainloop:
repeat on error undo mainloop, leave mainloop:
  
 
   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "re/"
       m_archpath    = m_archpath + "re/"
       m_successpath = m_successpath + "re/"
       m_errorpath   = m_errorpath + "re/"
       m_workpath    = m_workpath + "re/".


   empty temp-table ttproductionrs.
   
   
   /* Load SEARCH conditions from JSON  */ 
   
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
                    
  if vl_RetOK then do:

    for each ttSearchCondition:

           assign 
                  site          = ttSearchCondition.site_from
                  site1         = ttSearchCondition.site_to
                  prline        = ttSearchCondition.productionline_from
                  prline1       = ttSearchCondition.productionline_to 
                  release_date  = ttSearchCondition.date_from
                  release_date1 = ttSearchCondition.date_to 
                  . 

     end. /* for each ttSearchCondition. */

         if site1 = "" then site1 = hi_char.
         if prline1 = "" then prline1 = hi_char.
         if release_date = ? then release_date = low_date.
         if release_date1 = ? then release_date1 = hi_date.


         put unformatted prline skip
         site1  skip
         prline1  skip
         site   skip
         release_date1    skip
         release_date      skip
        .



        for each rps_mstr
        use-index rps_site_line
         where rps_mstr.rps_domain = global_domain 
          and rps_site     >= site
          and rps_site     <= site1
          and rps_line     >= prline
          and rps_line     <= prline1
          and rps_due_date >= release_date
          and rps_due_date <= release_date1
        no-lock,
      
           last lnd_det
           no-lock
               where lnd_det.lnd_domain = global_domain 
                and  lnd_site = rps_site
                and lnd_line      = rps_line
                and lnd_part      = rps_part
                and lnd_start    <= rps_rel_date
                // and lnd_run_seq1 >= primary_seq
                // and lnd_run_seq1 <= primary_seq1
                // and lnd_run_seq2 >= secondary_seq
                // and lnd_run_seq2 <= secondary_seq1
      
              break by rps_site
              by rps_line
              by rps_rel_date
            //   by lnd_run_seq1 {1}
            //   by lnd_run_seq2 {2}
              by lnd_part
              by rps_due_date:
      
        for first ln_mstr
            where ln_mstr.ln_domain = global_domain and  ln_site = rps_site
             and ln_line = rps_line
           no-lock:
        end. /* FOR FIRST ln_mstr */
      
        open_qty = rps_qty_req - rps_qty_comp.

        create ttproductionrs.
        assign
               ttproductionrs.schd_site         = rps_site
               ttproductionrs.schd_part         = rps_part 
               ttproductionrs.schd_line         = rps_line
               ttproductionrs.schd_rlse_date    = rps_rel_date
               ttproductionrs.schd_due_date     = rps_due_date
               ttproductionrs.schd_run_seq1     = lnd_run_seq1
               ttproductionrs.schd_run_seq2     = lnd_run_seq2
               ttproductionrs.schd_qty_req      = rps_qty_req
               ttproductionrs.schd_qty_comp     = rps_qty_comp
               ttproductionrs.schd_qty_open     = open_qty.
      
       
      
      
           end. /*   for each rps_mstr */

  end.  /* if vl_RetOK then do: */ 
                    
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
      
   
   
   leave.
end. /* repeat on error undo mainloop, leave mainloop: */
